
/******************************************************************************
 * MODULE     : QTMFileDialog.hpp
 * DESCRIPTION: QT file choosers
 * COPYRIGHT  : (C) 2009 David MICHEL
 *******************************************************************************
 * This software falls under the GNU general public license version 3 or later.
 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
 ******************************************************************************/

#ifndef QTMFILEDIALOG_HPP
#define QTMFILEDIALOG_HPP

#include "qt_utilities.hpp"
#include "string.hpp"
#include "sys_utils.hpp"
#include "tm_sys_utils.hpp"
#include <QFileDialog>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>

class QMyFileDialog : public QFileDialog {
  Q_OBJECT
public:
  QMyFileDialog (QWidget* parent= 0, const QString& caption= QString (),
                 const QString& directory= QString (),
                 const QString& filter   = QString ())
      : QFileDialog (parent, caption, directory, filter) {
    // If the current directory is $TEXMACS_PATH/bin, we should reset it to home
    if (os_win ()) {
      url current_dir=
          url_system (from_qstring ((this->directory ()).absolutePath ()));
      if (current_dir == get_texmacs_path () * "bin") {
        this->setDirectory (QDir::home ());
      }
    }
  }
};

class QTMFileDialog : public QDialog {
  Q_OBJECT

protected:
  QHBoxLayout* hbox;
  QFileDialog* file;
  void         dragEnterEvent (QDragEnterEvent* event);
  void         dragMoveEvent (QDragMoveEvent* event);
  void         dragLeaveEvent (QDragLeaveEvent* event);
  void         dropEvent (QDropEvent* event);

public:
  QTMFileDialog (QWidget* parent= 0, const QString& caption= QString (),
                 const QString& directory= QString (),
                 const QString& filter   = QString ());
  QStringList selectedFiles () { return file->selectedFiles (); };
#if defined(Q_OS_MAC)
  void setOptions (QFileDialog::Options opts) { file->setOptions (opts); };
#endif
  void setAcceptMode (QFileDialog::AcceptMode mode) {
    file->setAcceptMode (mode);
  }
  void setViewMode (QFileDialog::ViewMode mode) { file->setViewMode (mode); }
  void setFileMode (QFileDialog::FileMode mode) { file->setFileMode (mode); }
  void setNameFilter (const QString& filter) { file->setNameFilter (filter); }
  void setNameFilters (const QStringList& filters) {
    file->setNameFilters (filters);
  }
  void setDefaultSuffix (const QString& suffix) {
    file->setDefaultSuffix (suffix);
  }
  void setLabelText (QFileDialog::DialogLabel label, const QString& text) {
    file->setLabelText (label, text);
  }
};

class QTMImagePreview : public QWidget {
  Q_OBJECT

  QLabel* image;

public:
  QLineEdit* wid;
  QLineEdit* hei;
  QLineEdit* xps;
  QLineEdit* yps;

public slots:
  void setImage (const QString&);

public:
  QTMImagePreview (QWidget* parent= 0);
};

class QTMImageDialog : public QTMFileDialog {
  Q_OBJECT

  QTMImagePreview* preview;

public:
  QTMImageDialog (QWidget* parent= 0, const QString& caption= QString (),
                  const QString& directory= QString (),
                  const QString& filter   = QString ());
  string getParamsAsString ();
};

#endif // defined QTMFILEDIALOG_HPP
